package com.ttai.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerAddAcountComponet;
import com.ttai.dagger.module.activity.AddAcountPresenterModule;
import com.ttai.presenter.activity.AddAcountPresenter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.untils.Constant;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddAcount extends BaseActivity {
    @Bind(R.id.et_acountname)
    EditText etAcountname;
    @Bind(R.id.et_acountid)
    EditText etAcountid;
    @Bind(R.id.et_acountpassword)
    EditText etAcountpassword;
    @Bind(R.id.tb_password_visibility)
    ToggleButton tbPasswordVisibility;
    @Bind(R.id.btn_China)
    Button btnChina;
    @Bind(R.id.btn_America)
    Button btnAmerica;

    @Inject
    AddAcountPresenter addAcountPresenter;

    public static String acountName = "";
    public String acountId;
    public String acountPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addacount);

        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView barTitle = (TextView)findViewById(R.id.barTitle);
        barTitle.setText("账号密码加密");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        ButterKnife.bind(this);
        tbPasswordVisibility.setOnCheckedChangeListener(new ToggleButtonClick());
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
        setComponet();


    }

    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {
        finish();
    }

    @Override
    public void setComponet() {
        DaggerAddAcountComponet.builder()
                .addAcountPresenterModule(new AddAcountPresenterModule(this))
                .build()
                .in(this);
    }

    @OnClick({R.id.btn_China, R.id.btn_America})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_China:
                acountName = etAcountname.getText().toString();
                acountId = etAcountid.getText().toString();
                acountPassword = etAcountpassword.getText().toString();

                addAcountPresenter.storeTipAcount(Constant.getToken());
//                addAcountPresenter.dealDB();
                break;
            case R.id.btn_America:
                acountName = etAcountname.getText().toString();
                acountId = etAcountid.getText().toString();
                acountPassword = etAcountpassword.getText().toString();

                addAcountPresenter.storeTipAcount(Constant.getToken());
                break;
        }
    }

    private class ToggleButtonClick implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            //5、判断事件源的选中状态
            if (isChecked){
                //显示密码
                //etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                etAcountpassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }else {
                // 隐藏密码
                //etPassword.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
                etAcountpassword.setTransformationMethod(PasswordTransformationMethod.getInstance());

            }
            //6、每次显示或者关闭时，密码显示编辑的线不统一在最后，下面是为了统一
            etAcountpassword.setSelection(etAcountpassword.length());
        }
    }
}
